package com.qdu.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qdu.entity.Homemessage;
import com.qdu.entity.Housetype;
import com.qdu.entity.Users;
import com.qdu.service.AdministratorsService;
import com.qdu.service.CotenancyService;
import com.qdu.service.HousetypeService;
import com.qdu.service.ReportService;
import com.qdu.service.UsersService;
import com.qdu.entity.Administrators;
import com.qdu.entity.Collections;

@Controller
public class AdministratorsController {
	@Autowired
	private CotenancyService cotenancy;
   @Autowired
   private AdministratorsService admin;
   @Autowired
   private UsersService users;
   @Autowired
   private HousetypeService home;
   @Autowired
   private ReportService report;
	@GetMapping("/Administrators")
	public String comment() {
		
		return "Administrators";
	}
	  @GetMapping("/Adminindex/{Uid}")
	    public String toIndex(@PathVariable String Uid,Model model) {
	    	model.addAttribute("user",admin.getOneByAid(Uid));
	    	return "Adminindex";
	    }
     @PostMapping("/Administrators/login")
	    public String adlogin(String Aid,String Apwd,HttpSession session)
	    {
    	   Administrators ad=admin.getOneByAid(Aid);

	    	if(ad.getApwd().equals(Apwd))
	    	{
	    		session.setAttribute("session", ad);
	   
	    	   return"Adminindex";
	    	}
	    	else {
	    		return "Upwderror";
	    	}
	    }
	
     @GetMapping("/Administrators/AdminListCotenancy")
     public String AdminListCotenancy(Model model) {
     	model.addAttribute("cotelist",cotenancy.getAllCotenancy());
     	return "AdminListCotenancy";
     }
     @PostMapping("/Administrators/delete")
     @ResponseBody
     public void deleteCotenancy(String Uid,String Hid) {

         cotenancy.deleteCotenancy(Uid, Hid);
     }
     
     @PostMapping("/Administrators/deleteuser")
     @ResponseBody
     public void deleteuser(String Uid) {

         users.deleteUsers(Uid);
     }
     @PostMapping("/Administrators/deletehome")
     @ResponseBody
     public void deletehome(String Uid,String Hid) {

         home.deleteHouse(Uid, Hid);
     }
     @PostMapping("/Administrators/deletereport")
     @ResponseBody
     public void deletereport(String Uid) {

        report.deleteReport(Uid);
     }
     @PostMapping("/Administrators/banuser")
     @ResponseBody
     public void banuser(String Uid) {
         users.updateuatt2(Uid);
       
     }
     @PostMapping("/Administrators/unbanuser")
     @ResponseBody
     public void unbanuser(String Uid) {
         users.updateuatt1(Uid);
       
     }
     @PostMapping("/Administrators/passHome")
     @ResponseBody
     public void passHome(String Uid,String Hid) {
      Housetype a=home.gethousebyuidandhid(Uid, Hid);
      a.setHatt("2");
      home.updateHouse(a);
       
     }
     @GetMapping("/Administrators/AdminListUsers")
     public String AdminListUsers(Model model) {
     	model.addAttribute("users",users.getall1());
     	return "AdminListUsers";
     }
     @GetMapping("/Administrators/AdminListHome")
     public String AdminListHome(Model model) {
     	model.addAttribute("home",home.getallHouseList());
     	return "AdminListHome";
     }
     @GetMapping("/Administrators/AdminListBanHome")
     public String AdminListBanHome(Model model) {
     	model.addAttribute("home",home.getHouseByatt("1"));
     	return "AdminListBanHome";
     }
     @GetMapping("/Administrators/AdminListBanUsers")
     public String AdminListunUsers(Model model) {
     	model.addAttribute("users",users.getall2());
     	return "AdminListBanUsers";
     }
     
     @GetMapping("/Administrators/Report")
     public String AdminReport(Model model) {
     	model.addAttribute("report",report.getAll());
     	return "AdminReport";
     }
     
     @GetMapping("/Admin/toedithomepage/{Uid}/{Hid}")
     public String toedithomepage(@PathVariable String Uid,@PathVariable String Hid,Model model) {
    	 Housetype housetype=home.gethousebyuidandhid(Uid, Hid);
    	 model.addAttribute("house",housetype);
    	 return "edithouse";
     }
     @PostMapping("/Admin/edithouse")
     public String edituser(Housetype house,Model model,HttpSession session) {
     		home.updateHouse(house);
     		return "Adminindex";
     }

     @GetMapping("/editUser/{Uid}")
     public String FindUserbyuid(@PathVariable String Uid,Model model)
     {
     	model.addAttribute("user",users.getUserById(Uid));
     	return "editUser";
     }
     
     @GetMapping("/editUser2/{Uid}")
     public String toUser(@PathVariable String Uid,HttpSession session) {
     	session.setAttribute("session", Uid);
    	 return "Adminindex";
     }
}


